package com.nanyang.tms.service.forecast.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.invoker.TransOutboundInvoker;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.forecast.ForecastInfoAddRequest;
import com.nanyang.tms.api.dto.request.waybill.CalWeightRequest;
import com.nanyang.tms.api.dto.response.forecast.ForecastDetailResponse;
import com.nanyang.tms.api.dto.response.forecast.ForecastDetailVolumeResponse;
import com.nanyang.tms.api.dto.response.waybill.WeightResponse;
import com.nanyang.tms.api.service.forecast.ForecastCoreRpcService;
import com.nanyang.tms.domain.forecast.ForecastDetailVO;
import com.nanyang.tms.manager.forecast.ForecastManager;
import com.nanyang.tms.service.forecast.impl.flow.ForecastProcessPipelineFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class ForecastCoreRpcServiceImpl implements ForecastCoreRpcService{
	
   @Autowired	
   private ForecastProcessPipelineFactory  forecastProcessPipelineFactory;
   @Autowired
   private ForecastManager forecastManager;
   
	@Override
	public ApiResult<Boolean> insertForecast(ForecastInfoAddRequest forecastInfoAddRequest) {
		ApiResult<Boolean> apiResult= new ApiResult<Boolean>();
		try {
			TransOutboundInvoker build = forecastProcessPipelineFactory.build(forecastInfoAddRequest);
			build.start();
			TransHandlerContext context = build.getContext();
			System.out.println(context.toString());
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(true);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", forecastInfoAddRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}
	@Override
	public ApiResult<Boolean> updateTicketNumber(String forecastCode, String ticketNo) {
		ApiResult<Boolean> apiResult= new ApiResult<Boolean>();
		try {
			forecastManager.updateTicketNo(forecastCode, ticketNo);
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(true);
		} catch (Exception e) {
			log.error("inputParams:{} {} and errorMessage:{}", forecastCode,ticketNo, e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		
		return apiResult;
	}
	@Override
	public ApiResult<ForecastDetailResponse> getForecastDetail(String ticketNo) {
		ApiResult<ForecastDetailResponse> apiResult= new ApiResult<ForecastDetailResponse>();
		try {
			ForecastDetailResponse res=new ForecastDetailResponse();
			List<ForecastDetailVolumeResponse> volumes=new ArrayList<ForecastDetailVolumeResponse>();
			ForecastDetailVO forecastDetail = forecastManager.getForecastDetail(ticketNo);
			BeanUtils.copyProperties(forecastDetail, res);
			BeanUtils.copyListProperties(forecastDetail.getVolumes(), volumes, ForecastDetailVolumeResponse.class);
			res.setVolumes(volumes);
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(res);
		} catch (Exception e) {
			log.error("inputParams:{}  and errorMessage:{}", ticketNo, e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}

}

